package com.bingxue.edu.management.shop.service;

import com.bingxue.edu.common.util.EduUtil;
import com.bingxue.edu.framework.mybatis.base.BaseService;
import com.bingxue.edu.management.education.model.entity.ClassInfo;
import com.bingxue.edu.management.education.service.ClassInfoService;
import com.bingxue.edu.management.education.service.EnrollPermissionService;
import com.bingxue.edu.management.shop.mapper.CartMapper;
import com.bingxue.edu.management.shop.model.entity.Cart;
import com.bingxue.edu.parent.model.req.ParentCartItemAddReq;
import com.bingxue.edu.parent.model.resp.ParentCartItem;
import com.bingxue.edu.parent.service.ParentShopService;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.bingxue.edu.management.shop.model.entity.table.CartTableDef.CART;

@Slf4j
@Service
@RequiredArgsConstructor
public class CartService extends BaseService<CartMapper, Cart> {

    @Lazy
    @Resource
    private ParentShopService parentShopService;
    @Lazy
    @Resource
    private ClassInfoService classInfoService;
    @Lazy
    @Resource
    private EnrollPermissionService enrollPermissionService;

    @Transactional(rollbackFor = Exception.class)
    public void addCartItem(ParentCartItemAddReq req, boolean checkPermission) {
        Cart cart = new Cart();
        BeanUtils.copyProperties(req, cart);
        boolean exists = queryChain().where(CART.STUDENT_ID.eq(cart.getStudentId()))
                .and(CART.CLASS_ID.eq(cart.getClassId())).exists();
        if (exists) {
            log.warn("购物车中已存在该班级, 忽略");
            return;
        }
        if(checkPermission) {
            ClassInfo classInfo = classInfoService.getById(req.getClassId());
            EduUtil.checkEnrollTime(classInfo);
            EduUtil.checkAvailableCount(classInfo);
            enrollPermissionService.checkPermission(req.getStudentId(), classInfo);
        }

        this.save(cart);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeCardItem(String id) {
        this.removeById(id);
        log.info("删除购物课程成功");
    }

    /**
     * 查询学生购物车列表
     */
    public List<ParentCartItem> getCardItemList(String studentId) {
        List<ParentCartItem> cartItemList = queryChain().where(CART.STUDENT_ID.eq(studentId)).listAs(ParentCartItem.class);
        cartItemList.forEach(item -> item.setClassInfo(parentShopService.getShopClassById(item.getClassId())));
        return cartItemList;
    }

    public long getCardItemCount(String studentId) {
        return queryChain().where(CART.STUDENT_ID.eq(studentId)).count();
    }

    public void deleteItemByClassId(String classId) {
        this.remove(queryChain().where(CART.CLASS_ID.eq(classId)));
    }

    public void deleteItemByStudentAndClassId(String studentId, String classId) {
        this.remove(queryChain().where(CART.STUDENT_ID.eq(studentId)).and(CART.CLASS_ID.eq(classId)));
    }
}
